# -*- coding: utf-8 *-*

#   This file is part of Sql-Rice.
#
#    Sql-Rice is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 3 of the License, or
#    (at your option) any later version.
#
#    Sql-Rice is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with Sql-Rice; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

import unittest
import rules.models


class TestValidations(unittest.TestCase):

    def setUp(self):
        rules.models.metadata.bind = 'sqlite:///:memory:'

        rules.models.setup_all(True)
        v = rules.models.Validator(name=u'rut', rules=[])
        f = rules.models.Validator(name=u'date', rules=[])
        n = rules.models.Validator(name=u'natural', rules=[])
        m = rules.models.Validator(name=u'Maximum', params=100, rules=[])
        rules.models.Rule(pattern=u'rut', apply_to=u'fields', validators=[v],
                          default=u'replace')
        rules.models.Rule(pattern=u'fecha', apply_to=u'fields',
                          validators=[f], default=u'replace')
        rules.models.Rule(pattern='edad', apply_to=u'fields',
                          validators=[m, n], default=u'replace')
        rules.models.session.commit()

    def test_validate_rut(self):
        self.assertItemsEqual([rules.valide.rut], rules.find(u'rut'))

    def test_validate_nombre(self):
        self.assertItemsEqual([], rules.find(u'nombre'))

    def test_validate_fecha(self):
        self.assertListEqual([rules.valide.date], rules.find(u'fecha'))

    def test_valide_rango_y_entero(self):
        self.assertListEqual([rules.valide.natural],
                               rules.find(u'edad'))
